package pers.xx.sbm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.xx.sbm.entity.Permission;
import pers.xx.sbm.entity.User;
import pers.xx.sbm.service.PermissionService;
import pers.xx.sbm.utils.UserUtils;
import pers.xx.sbm.vo.ResResult;

import java.util.Date;

/**
 * @author ：xiexing
 * @description：权限列表操作Controller
 * @date ：2020/8/23 21:19
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @Autowired
    private UserUtils userUtils;


    /**
     * @description 权限管理
     * @author XieXing
     * @date 2020/8/11 15:45
     */
    @RequestMapping("/")
    public String menuPage(Model model) {
        return "page/system/permission";
    }

    /**
     * @description 获取权限列表
     * @author XieXing
     * @date 2020/8/12 14:52
     */
    @RequestMapping("/getMenuList")
    @ResponseBody
    public ResResult getMenusList(Permission permission, Integer page, Integer limit, String permissionSearchContent) {
        if (!StringUtils.isEmpty(permissionSearchContent)) {
            permission.setPermissionName(permissionSearchContent);
        }
        ResResult resResult = permissionService.listByPage(permission, page, limit);
        resResult.setOptName("获取权限列表");
        return resResult;
    }

    /**
     * @description 保存权限信息
     * @author XieXing
     * @date 2020/8/23 21:35
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResResult saveMenu(Permission permission) {
        User user = userUtils.getCurrentUser();
        permission.setPermissionAddby(user.getUserName());
        permission.setPermissionAddbyId(user.getId());
        permission.setPermissionAddTime(new Date());
        permission.setPermissionIsEnable(1);
        ResResult resResult = permissionService.insertSelective(permission);
        resResult.setOptName("保存权限信息");
        return resResult;
    }


    /**
     * @description 更新权限信息
     * @author XieXing
     * @date 2020/8/23 22:01
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResResult updateMenu(Permission permission) {
        User user = userUtils.getCurrentUser();
        permission.setPermissionAddby(user.getUserName());
        permission.setPermissionUpdatebyId(user.getId());
        permission.setPermissionUpdateTime(new Date());
        ResResult resResult = permissionService.updateByPrimaryKeySelective(permission);//
        resResult.setOptName("更新权限信息");
        return resResult;
    }

    /**
     * @description 更新权限信息
     * @author XieXing
     * @date 2020/8/23 22:01
     */
    @RequestMapping("/{id}")
    @ResponseBody
    public ResResult delMenu(@PathVariable(value = "id") Integer id) {
        ResResult resResult = permissionService.deleteByPrimaryKey(id);//
        resResult.setOptName("删除权限信息");
        return resResult;
    }


    /**
     * @description 更新权限状态
     * @author XieXing
     * @date 2020/8/23 21:24
     */
    @RequestMapping("/{id}/{status}")
    @ResponseBody
    public ResResult updateDataStatus(@PathVariable(value = "id") Integer id, @PathVariable(value = "status") Integer status) {
        //TODO 从session获取当前操作用户信息
        ResResult resResult = permissionService.updateDataStatus(id, status);
        resResult.setOptName("更新权限状态");
        return resResult;
    }
}
